package com.tommy.jiami.utils.task;

import android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import android.widget.*;
import com.tommy.jiami.utils.*;
import java.io.*;

public class CryptFileTask extends AsyncTask<String,Integer,Boolean>
	{
		Context context;
		int type;
		ProgressDialog dialog;
		boolean isDelete=false;

		public CryptFileTask(Context context,int type,boolean isDelete)
		{
			this.context = context;
			this.type=type;
			this.isDelete=isDelete;
		}

		@Override
		protected void onPreExecute()
		{
			dialog=new ProgressDialog(context);
			dialog.setTitle("正在"+Utils.getCryptType(type)+"中...");
			dialog.setCanceledOnTouchOutside(false);
			dialog.setButton("隐藏",new DialogInterface.OnClickListener(){
					public void onClick(DialogInterface dialog,int what){
						dialog.cancel();
					}
				});
			dialog.setMessage("正在对文件进行"+Utils.getCryptType(type)+"，请等待......");
			dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			dialog.show();
		}

		@Override
		protected Boolean doInBackground(String[] params)
		{
			try
			{
				switch(type){
					case Crypt.TYPE_ENCRYPT:
						Crypt.encryptFile(params[0],params[1],params[2],isDelete);
						break;
					case Crypt.TYPE_DECRYPT:
						Crypt.decryptFile(params[0],params[1],params[2],isDelete);
						break;
				}
				return true;
			}
			catch (Exception e){
				new File(params[1]).delete();
				Log.e("AndroidRuntime",e.getMessage());
				return false;
			}
		}

		@Override
		protected void onPostExecute(Boolean result)
		{
			dialog.cancel();
			if(result){
				Toast.makeText(context,Utils.getCryptType(type)+"成功！",1).show();
				context.sendBroadcast(new Intent(Constants.ACTION_CRYPT_OK));			
			}
			else Toast.makeText(context,Utils.getCryptType(type)+"失败！",1).show();
		}

	}
